﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DivisonsCodeTools.Business.Configure
{
    public class AppConfig
    {
        private static string path = Application.StartupPath + "\\AppConfig.xml";

        /// <summary>
        /// 判断AppConfig是否存在 
        /// </summary>
        /// <returns></returns>
        public static bool IsAppConfig()
        {
            bool isExists = File.Exists(path);
            if (isExists)
            {
                Read();
            }
            return isExists;
        }

        /// <summary>
        /// 初始化AppConfig
        /// </summary>
        /// <returns></returns>
        public static bool InitConfig()
        {
            bool sucess = false;
            Type t = typeof(ConfigInfo);
            XmlTextWriter xml = new XmlTextWriter(path, Encoding.UTF8);
            xml.Formatting = Formatting.Indented;
            xml.WriteStartDocument();
            xml.WriteStartElement("AppConfig");
            foreach (var item in t.GetProperties())
            {
                xml.WriteElementString(item.Name, null);
            }
            xml.WriteEndElement();
            xml.WriteEndDocument();
            xml.Flush();
            xml.Close();
            sucess = true;
            return sucess;
        }

        /// <summary>
        /// 读取xml配置
        /// </summary>
        /// <returns></returns>
        public static ConfigInfo Read()
        {
            Type t = typeof(ConfigInfo);
            foreach (var item in t.GetProperties())
            {
                item.SetValue(Config.GetInstance(), XmlHelper.Read(path, "//AppConfig//" + item.Name, null), null);
            }

            return Config.GetInstance();
        }

        /// <summary>
        /// 保存xml配置
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool Save(ConfigInfo info)
        {
            Type t = typeof(ConfigInfo);
            foreach (var item in t.GetProperties())
            {
                string temvalue = item.GetValue(info, null).ToString();
                XmlHelper.Update(path, "//AppConfig//" + item.Name, "", temvalue);
            }
            return true;
        }
    }


    public class ConfigInfo
    {
        private string _fileName;
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get
            {
                if (string.IsNullOrEmpty(_fileName))
                    return "Divison.json";
                return _fileName;
            }
            set
            {
                _fileName = value;
            }
        }

        private string _filePath;
        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_filePath))
                    return "D:\\";
                return _filePath;
            }
            set
            {
                _filePath = value;
            }
        }


        public string _url;
        /// <summary>
        /// 采集地址
        /// </summary>
        public string Url 
        { 
            get
            {
                if(string.IsNullOrEmpty(_url))
                    return "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm";
                return _url;
            }
            set
            {
                _url = value;
            }
        }

        private string _mariadbConnectionString;
        /// <summary>
        /// Mariadb连接字符串 
        /// </summary>
        public string MariadbConnectionString 
        { 
            get
            {

                if (string.IsNullOrEmpty(_mariadbConnectionString))
                    return "server=Localhost;user id=root;password=123456;database=officehelper";
                return _mariadbConnectionString;
            }
            set
            {
                _mariadbConnectionString = value;
            }

        }
        private string _mariadbTableName;
        public string MariadbTableName 
        { 
            get
            {
                if(string.IsNullOrEmpty(_mariadbTableName))
                    return "Divison";
                return _mariadbTableName;
            }
            set
            {
                _mariadbTableName = value;
            }
        }
    }


    public class Config
    {
        private Config()
        {

        }
        private static readonly ConfigInfo instance = new ConfigInfo();
        /// <summary>
        /// 获取AppConfig的对象信息 
        /// </summary>
        /// <returns></returns>
        public static ConfigInfo GetInstance()
        {
            return instance;
        }
    }
}
